package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {

        checkGroupDao.add(checkGroup);

        writeRelation(checkGroup, checkitemIds);

    }

    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);

        Page<CheckGroup> all = checkGroupDao.findAll(queryString);

        return new PageResult(all.getTotal(),all.getResult());
    }

    @Override
    public CheckGroup findById(Integer id) {

        CheckGroup checkGroup = checkGroupDao.findById(id);

        return checkGroup;
    }

    @Override
    public List<CheckItem> findAllCheckItem() {
        List<CheckItem> list = checkGroupDao.findAllCheckItem();
        return list;
    }

    @Override
    public List<Integer> findContactByCheckGroupId(Integer checkGroupId) {
        List<Integer> CheckItemId = checkGroupDao.findContactByCheckGroupId(checkGroupId);
        return CheckItemId;
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //修改 组
        checkGroupDao.deit(checkGroup);

        //清空关联表
        checkGroupDao.emptyContact(checkGroup.getId());

        //从新写入关系
        writeRelation(checkGroup, checkitemIds);
    }

    //删除 检查组
    @Override
    public void delete(Integer id) {
        //删除之前判断前后所有关联表是否有关联
        Long aLong = checkGroupDao.countCheckgroupAndCheckitem(id);
        Long aLong1 = checkGroupDao.countSetmealAndCheckgroup(id);

        if (aLong > 0 || aLong1 > 0) {
            throw new RuntimeException("有关联表占用!");
        } else {
            //没有关联表删除
            checkGroupDao.delete(id);
        }
    }



    //公共 方法 写入 中间表
    private void writeRelation(CheckGroup checkGroup, Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkItemId : checkitemIds) {
                Map<Object, Object> map = new HashMap<>();
                map.put("checkItemId",checkItemId);
                map.put("checkGroupId",checkGroup.getId());

                checkGroupDao.addCheckItemAndCheckGroup(map);
            }
        }
    }


    @Override
    public List<CheckGroup> findAllCheckGroup() {
        List<CheckGroup> list = checkGroupDao.findAllCheckGroup();
        return list;
    }
}
